﻿using MailDAL;
using MailTool.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MailTool.Pages
{
    public partial class MailAddressEdit : System.Web.UI.Page
    {
        UnitOfWork unitOfWork = new UnitOfWork();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                InitPageData();
            }
        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void InitPageData()
        {
            if (Action == "add")
            {
                lblCreateDate.Text = DateTime.Now.ToString("yyyy-MM-dd hh:mm");
            }
            else
            {
                MailModel.AddressList addressList = unitOfWork.AddressListRepository.Get(p => p.Id == AddressListID, null, 1, 0).FirstOrDefault();
                txtUserName.Text = addressList.UserName;
                txtAddress.Text = addressList.Address;
                txtTag.Text = addressList.Tag;
                lblCreateDate.Text = addressList.CreateDate.ToString("yyyy-MM-dd hh:mm");
            }
        }

        protected void BtnEditArticle_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtUserName.Text))
            {
                this.LblError.Text = "<font color='red'>姓名不能为空！</font>";
                return;
            }
            else if (string.IsNullOrEmpty(txtAddress.Text))
            {
                this.LblError.Text = "<font color='red'>E-mail不能为空！</font>";
                return;
            }
            Regex r = new Regex("^\\s*([A-Za-z0-9_-]+(\\.\\w+)*@(\\w+\\.)+\\w{2,5})\\s*$");
            if (!r.IsMatch(txtAddress.Text))
            {
                this.LblError.Text = "<font color='red'>E-mail格式不正确！</font>";
                return;
            }

            try
            {
                if (Action == "add")
                {
                    MailModel.AddressList addressList = new MailModel.AddressList();
                    addressList.UserName = txtUserName.Text.Trim();
                    addressList.Address = txtAddress.Text.Trim();
                    addressList.Tag = txtTag.Text.Trim();
                    addressList.CreateDate = DateTime.Now;
                    addressList.OwnerId = SessionState.CurrentUser.Id;
                    unitOfWork.AddressListRepository.Insert(addressList);
                    unitOfWork.Save();
                }
                else
                {
                    MailModel.AddressList addressList = unitOfWork.AddressListRepository.Get(p => p.Id == AddressListID, null, 1, 0, x => x.User).FirstOrDefault();
                    addressList.UserName = txtUserName.Text.Trim();
                    addressList.Address = txtAddress.Text.Trim();
                    addressList.Tag = txtTag.Text.Trim();
                    addressList.CreateDate = DateTime.Now;
                    unitOfWork.Save();
                }
                JsUtil.AlertConfirm("活动保存成功！", this.Page);
                JsUtil.RefreshParent("MailAddressList.aspx", this.Page);
            }
            catch
            {
                JsUtil.Alert("保存失败！", this.Page);
            }
        }

        protected int AddressListID
        {
            get
            {
                return Convert.ToInt32(base.Request["addressListId"]);
            }
        }

        protected string Action
        {
            get
            {
                return base.Request["action"];
            }
        }

        protected MailModel.User CurrentUser
        {
            get
            {
                return SessionState.CurrentUser;
            }
        }
    }
}